#include "MenuState.h"

MenuState::MenuState(){
	active = true;
}

MenuState::~MenuState(){
}

//mengubah input berupa token menjadi informasi untuk ditampilkan
DisplayInfo MenuState::update(CommandToken commandToken){
	displayInfo.message.clear();
	displayInfo.prompt.clear();
	displayInfo.state=APP_STATE;

	//melakukan aksi berdasarkan kode dari command dari user
	switch (commandToken.command){
		case EXIT_COMMAND_CODE :{
			//non aktifkan state dan menampilkan pesan
			active = false;
			displayInfo.message.push_back("Closing applications...");
			displayInfo.message.push_back("See ya! :)");
			displayInfo.state=EXIT_STATE;
			break;}

		case LOADGAME_COMMAND_CODE :{
			//masuk ke gamestate
			displayInfo.state=GAME_STATE;
			//mengambil informasi yang telah di save dan memasukkan ke tipe bentukkan SaveInfo

			SaveInfo saveInfo = Resource::instance() -> loadGame(commandToken.stringParam[0]+SAVE_FILE_EXTENSION);
            //membuat game baru berdasarkan info tersebut
			if (saveInfo.status == STATUS_FOUND){
                p_App->addState(new GameState(saveInfo));
                displayInfo = p_App->getFirstDisplay();
                displayInfo.message.push_back("Loading data...");
                displayInfo.message.push_back("Loading completed.\n");
			} else if (saveInfo.status == STATUS_EMPTY){
                    displayInfo = p_App->getFirstDisplay();
                    displayInfo.message.push_back("Cannot Load data...");
                    displayInfo.message.push_back("File Is Empty\n");
			} else if (saveInfo.status == STATUS_NULL){
                    displayInfo = p_App->getFirstDisplay();
                    displayInfo.message.push_back("Cannot Load data...");
                    displayInfo.message.push_back("File Not Found \n");

			}
			break;}

		case NEWGAME_COMMAND_CODE :{
		    if (!commandToken.stringParam.empty()){
                //membuat game baru dengan melewatkan parameter nama player dan menginisialisasi game dengan nilai-nilai default
                p_App->addState(new GameState(commandToken.stringParam.front()));
                //mengambil tampilan pertama dari state yang baru
                displayInfo = p_App->getFirstDisplay();
                displayInfo.message.push_back("Creating new game...");
                displayInfo.message.push_back("New game is created.\n");
                string welcome;
                welcome.append("Welcome ");
                welcome.append(commandToken.stringParam.front());
                welcome.append("!");
                displayInfo.message.push_back(welcome);
		    } else {
                displayInfo.message.push_back("You need insert player name!");
		    }
			break;}

		default :
			//bila command tidak dikenali
			displayInfo.message.push_back("Command doesn't recognized");
		break;
	}
	//mengembalikan informasi untuk ditampilkan
	return displayInfo;
}

//mendapatkan display default pertama kali (bukan dari user)
DisplayInfo MenuState::getFirstDisplay(){
	DisplayInfo displayInfo;
	displayInfo.message.clear();
	displayInfo.prompt.clear();
	displayInfo.state=APP_STATE;
	return displayInfo;
}
